<?php
/*******************************
 * $URL: $
 * $Date:  2011-12-27 $
 * $Revision: 10 $
 * $Author: vsoria@alertia.net $
 ******************************/

/*******************************
 * AlertiaClient: PHP Alertia API client class
 * http://www.alertia.net
 ******************************/

/***********************************************************
Authors: Vicent Soria Durá <vsoria@alertia.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License  Version 2,
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You can obtain a copy of the GNU General Poblic License Version 2
from http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
***********************************************************/


class AlertiaAPIClient {
	private $endpoint = 'https://pre.api.alertia.net/v1'; // the url for the api service

	private $key = ''; // the api key

	private $err_code = NULL; // the error code from the last api call
	private $err_msg  = NULL; // the error message from the last api call


	function __construct($key = null, $endpoint = null) {
		if(isset($endpoint)) {
			$this->endpoint = $endpoint;
		}
		
		$this->key = $key;
		$this->err_code = NULL;
		$this->err_msg = NULL;
	}


	/** GETTERS/SETTERS **/
	function set_endpoint($value)	{	$this->endpoint = $value;		}
	function set_key($value) 		{	$this->key = (string) $value;	}

	protected function set_error_code($value)	{	$this->err_code = $value; 	}
	protected function set_error_message($value){	$this->err_msg = $value; 	}

	function get_error_code()		{	return $this->err_code; 	}
	function get_error_message()	{	return $this->err_msg;		}

	protected function clear_response_codes(){
		$this->err_code = NULL;
		$this->err_msg = NULL;
	}

/**
 * get apikey
 * 
 * @param string $username
 * @param string $password
 * 
 * @return bool
 */
	function fetch_key($username, $password){
		$url = $this->endpoint.'/apikey/get';

		$response = $this->real_connect_api($url, array('login' => $username, 'password' => $password), false );
		
		if(!$response)
			return false;

		$this->key = $response['data']['apikey'];
		return true;
	}

/**
 * connection call to api
 * 
 * @param string $method
 * @param array $post
 * @param bool $compression
 * 
 * @return false|object
 */
	function connect_api($method, $post = array(), $compression = true ){
		$this->clear_response_codes();

		if(empty($this->key)) {
			$this->set_error_code(-1);
			$this->set_error_message('no authentication key specified');
			
			return false;
		}

		$get = array();
		$get['apikey'] = $this->key;

		$url = $this->endpoint.$method.'?'.http_build_query($get, '', '&');
		$post = array('params' => json_encode($post));
		
		return $this->real_connect_api($url, $post, true);
	}
	
/**
 * Real connection with api
 * 
 * @param string $url
 * @param array $post
 * @param bool $compression
 * 
 * @return false|object
 */
	protected function real_connect_api($url, $post = array(), $compression = true){
		// curl session
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Alertia API v1 Client ');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

		if( $compression === true )
			curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
		
		$response = curl_exec($ch);
		$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$curlerrcode = curl_errno($ch);
		$curlerr = curl_error($ch);
		
		curl_close($ch);
		
		// check for errors
		if($curlerrcode != 0) {
			$this->set_error_code($curlerrcode);
			$this->set_error_message($curlerr);
			return false;
		}
		if($httpcode != 200) {
			$this->set_error_code($httpcode);
			$this->set_error_message('http error');
			return false;
		}
		$object = json_decode($response, true);
		if($object['success'] == 0) {
			$this->set_error_code($object['errorCode']);
			$this->set_error_message($object['msg']);
			
			return false;
		}
		
		return $object;	 
	}
}
